﻿using PureMVC.Interfaces;
using PureMVC.Patterns.Command;
using PureMVC.Patterns.Mediator;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShowPanelCommand : SimpleCommand
{
    //1.继承Command相关的脚本
    //2.重写里面的执行函数



    public override void Execute(INotification notification)
    {
        base.Execute(notification);
        //创建面板的逻辑
        string panelName = notification.Body.ToString();
        switch (panelName)
        {
            case "MainPanel":
                //显示面板相关
                //如果要使用Mediator 一定也要再Facade中去注册
                //command proxy也一样 要用就要注册
                //判断如果没有Mediator就去new一个
                if (!Facade.HasMediator(NewMainViewMediator.NAME))
                {
                    Facade.RegisterMediator(new NewMainViewMediator());
                }
                //有Mediator了就去创建界面创建预设体

                //Facade得到Mediator的方法
                NewMainViewMediator mm = Facade.RetrieveMediator(NewMainViewMediator.NAME) as NewMainViewMediator;

                if (mm.ViewComponent == null && mm != null)
                {

                    //实例化面板对象
                    GameObject res = Resources.Load<GameObject>("UI/MainPanel");
                    GameObject obj = Object.Instantiate(res);
                    //设置它的父对象 为Canvas
                    obj.transform.SetParent(GameObject.Find("Canvas").transform, false);

                    //得到预设体上的view脚本 关联到Mediator上
                    mm.SetView(obj.GetComponent<NewMainView>());
                }
                //往往显示了面板后就会要进行第一次更新
                SendNotification(PureNotification.UPDATE_PLAYER_INFO,Facade.RetrieveProxy(PlayerProxy.NAME).Data);

                break;
            case "RolePanel":
                //显示面板相关

                //如果要使用Mediator 一定也要再Facade中去注册
                //command proxy也一样 要用就要注册
                //判断如果没有Mediator就去new一个
                if (!Facade.HasMediator(NewRoleViewMediator.NAME))
                {
                    Facade.RegisterMediator(new NewRoleViewMediator());
                }
                //有Mediator了就去创建界面创建预设体

                //Facade得到Mediator的方法
                NewRoleViewMediator rm = Facade.RetrieveMediator(NewRoleViewMediator.NAME) as NewRoleViewMediator;

                if (rm.ViewComponent == null && rm != null)
                {
                    //实例化面板对象
                    GameObject res = Resources.Load<GameObject>("UI/RolePanel");
                    GameObject obj = Object.Instantiate(res);
                    //设置它的父对象 为Canvas
                    obj.transform.SetParent(GameObject.Find("Canvas").transform, false);

                    //得到预设体上的view脚本 关联到Mediator上
                    rm.SetView(obj.GetComponent<NewRoleView>());
                }
                SendNotification(PureNotification.UPDATE_PLAYER_INFO, Facade.RetrieveProxy(PlayerProxy.NAME).Data);

                break;
        }



    }




}
